package com.chase.framerwork.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

/**
 * 集合类的工具类
 * 
 * @Author Chase
 * @Date 2013-10-19
 */
public class CollectionUtil extends CollectionUtils
{
	/**
	 * 集合为空
	 * 
	 * @param collection
	 * @return
	 */
	public static boolean isEmpty(Collection<?> collection)
	{
		if (collection == null || collection.isEmpty())
		{
			return true;
		}
		return false;
	}

	/**
	 * 集合不为空
	 * 
	 * @param collection
	 * @return
	 */
	public static boolean isNotEmpty(Collection<?> collection)
	{
		return !isEmpty(collection);
	}

	/**
	 * 取两个集合的并集 (返回a加b的新List).
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static <T> List<T> unions(Collection<T> a, Collection<T> b)
	{
		List<T> result = new ArrayList<T>(a);
		result.addAll(b);
		return result;
	}

	/**
	 * 取两个集合的非交集 (返回a减b的新List).
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static <T> List<T> subtracts(Collection<T> a, Collection<T> b)
	{
		List<T> list = new ArrayList<T>(a);
		for (T element : b)
		{
			list.remove(element);
		}

		return list;
	}

	/**
	 * 取两个集合的交集(返回a和b的交集的新List).
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static <T> List<T> intersections(Collection<T> a, Collection<T> b)
	{
		List<T> list = new ArrayList<T>();

		for (T element : a)
		{
			if (b.contains(element))
			{
				list.add(element);
			}
		}
		return list;
	}
}
